// pages/myorder/myorder.js
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    orderData: {},
    recordArr: [],
    /**all 全部订单   0 未付款,1 待发货,2 待收货 */
    recordType: {},
    loading: { all: false, 0: false, 1: false, 2: false, },
    /**删除订单提示 */
    isEditSucceed: false,

    editOrdertype: false,
    ordertype: 0,
    arrlength: 0,
    /**提示 已提醒发货  已确认发货 */
    setGoodsTip: "已提醒发货",
    windowHeight: 1106,
    /**状态栏 */
    orderNav: [{ "name": "全部订单", "sel": true }, { "name": "待付款", "sel": false }, { "name": "待发货", "sel": false }, { "name": "待收货", "sel": false }]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**切换订单类型 */
  onClickNav: function (e) {
    let id = parseInt(e.currentTarget.dataset.index);

    for (let i = 0; i < this.data.orderNav.length; i++) {
      if (i == id) {
        this.data.orderNav[i]["sel"] = true;
      } else
        this.data.orderNav[i]["sel"] = false;
    }
    if (id == 0) {
      this.data.recordType = "all";
    } else {
      this.data.recordType = (id - 1);
    }
    this.setData({
      orderNav: this.data.orderNav,
      recordType: this.data.recordType,
    })
    this.getRecord(this.data.recordType, 1);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (app.globalData.windowHeight) {
      var height = app.globalData.windowHeight;
      this.data.windowHeight = height - height * 0.0487;
      this.setData({
        windowHeight: this.data.windowHeight
      })
    }

    this.data.recordType = GlobalData.recordType;
    let id = 0;
    if (isNaN(GlobalData.recordType)) {
      id = 0;
    } else {
      id = parseInt(this.data.recordType) + 1
    }
    for (let i = 0; i < this.data.orderNav.length; i++) {
      if (i == id) {
        this.data.orderNav[i]["sel"] = true;
      } else
        this.data.orderNav[i]["sel"] = false;
    }
    this.setData({
      orderNav: this.data.orderNav,
      recordType: this.data.recordType,
    });
    this.getRecord(this.data.recordType, 1)
  },
  /**获取订单记录完成后的回调函数 */
  onComplete() {
    this.data.recordArr = GlobalData.exchangeRecord[this.data.recordType];
    for (let i = 0; i < this.data.recordArr.length; i++) {
      if (this.data.recordArr[i]["createtime"]) {
        this.data.recordArr[i]["createtime"] = this.data.recordArr[i]["createtime"].replace("T", " ")
      }
      if (this.data.recordArr[i]["state"] == 0) {
        this.data.recordArr[i]["cancel"] = true;
        this.data.recordArr[i]["text"] = "立即支付"
      } else if (this.data.recordArr[i]["state"] == 1) {
        this.data.recordArr[i]["cancel"] = false;
        this.data.recordArr[i]["text"] = "提醒发货"
      } else if (this.data.recordArr[i]["state"] == 2) {
        this.data.recordArr[i]["cancel"] = false;
        this.data.recordArr[i]["text"] = "确认收货"
      } else if (this.data.recordArr[i]["state"] == 3) {
        this.data.recordArr[i]["cancel"] = false;
        this.data.recordArr[i]["text"] = "已收货"
      } if (this.data.recordArr[i]["state"] == -1) {
        this.data.recordArr[i]["cancel"] = false;
        this.data.recordArr[i]["text"] = "订单已取消"
      }

      for (let k = 0; k < this.data.recordArr[i].items.length; k++) {
        for (let j = 0; j < this.data.recordArr[i].items[k].img.length; j++) {
          if (this.data.recordArr[i].items[k].img[j].sortno == 3) {
            this.data.recordArr[i].items[k]['image'] = this.data.recordArr[i].items[k].img[j].path;
          }
        }
      }
    }
    this.setData({
      recordArr: this.data.recordArr,
    })
    if (GlobalData.exchangeRecord[this.data.recordType].length == this.data.arrlength)
      return;
    this.data.loading[this.data.recordType] = false;
    this.data.arrlength = this.data.recordArr.length;

    this.setData({
      recordArr: this.data.recordArr,
      loading: this.data.loading,
      arrlength: this.data.arrlength,
    })
  },
  getRecord(state, index) {
    dataManager.rigisterObserver("getordernos", this.onComplete, this);
    dataManager.getExchangeRecordDm(state, 1, 10, false);
  },
  /**滑动加载更多记录 */
  lower: function (e) {
    if (!this.data.loading[this.data.recordType]) {
      this.data.loading[this.data.recordType] = true;
      /**获取最近战绩的数据 */
      GlobalData.exchangeRecordPage[this.data.recordType] += 1;
      dataManager.rigisterObserver("getordernos", this.onComplete, this);
      dataManager.getExchangeRecordDm(this.data.recordType, GlobalData.exchangeRecordPage[this.data.recordType], 10, true);
    }
  },
  /**1：取消订单 2：提醒发货， 确认收货 */
  onEditOrder(e) {
    var that = this;
    let data = e.currentTarget.dataset.record;
    let index = parseInt(e.currentTarget.dataset.index);
    this.data.ordertype = data.state;
    switch (index) {
      case 1:
        // this.data.isEditSucceed = true;
        this.data.orderData = data;
        wx.showModal({
          title: "提示",
          content: "是否删除此订单？",
          showCancel: true,
          success: function (res) {
            if (res.confirm) {
              that.onConfirmCancelOrder(that.data.orderData)
            } else {
              that.data.isEditSucceed = false;
              that.data.orderData = null;
            }
          }
        })
        break;
      case 2:
        //0 为发货提醒 1 为确认收货提醒
        let type = 0
        switch (data.state) {
          case 0:

            if (GlobalData.homeData) {
              if (data.bonuspoints > GlobalData.homeData.bonuspoints) {
                app.onshowModal("积分不足。");
                return;
              }
              GlobalData.order = {};
              GlobalData.order["orderno"] = data.orderno;
              GlobalData.order["money"] = data.cash;
              GlobalData.order["bonuspoints"] = data.bonuspoints;
              GlobalData.order["time"] = data.createtime;
              GlobalData.PayReason = 5;
              /**支付页面 */
              dataManager.UseNavigateTo("/pages/payment/payment", false)
            }


            break;
          case 1:
            type = 0
            this.data.setGoodsTip = "已提醒商家发货"
            dataManager.rigisterObserver("remindshipping", this.onEditOrderComplete, this);
            dataManager.onEditOrderDm(data.orderno, type);
            break;
          case 2:
            type = 1
            this.data.setGoodsTip = "已确认收货"
            dataManager.rigisterObserver("remindshipping", this.onEditOrderComplete, this);
            dataManager.onEditOrderDm(data.orderno, type);
            break;
        }
    }
  },
  /**修改订单的完成的回调函数 */
  onEditOrderComplete() {
    var that = this;
    this.getRecord(this.data.recordType, 1);
    if (this.data.ordertype == 1 || this.data.ordertype == 2) {
      wx.showModal({
        title: '提示',
        content: that.data.setGoodsTip,
        showCancel: false,
        success: function (res) {

        }
      })
    }
    // if (this.data.ordertype == 1 || this.data.ordertype == 2) {
    //   this.data.editOrdertype = true;
    // } else
    //   this.data.isEditSucceed = false;
  },
  /**删除订单 */
  onEditCancel(index) {
    if (index == 1) {
      this.data.isEditSucceed = false;
      this.data.data = null;
    } else if (index == 2) {
      this.data.onConfirmCancelOrder(this.data.orderData)
    } else {
      this.data.editOrdertype = false;
    }
  },
  goDetails(e) {
    console.log(e);
    let id = e.currentTarget.dataset.item.detailid;
   dataManager.rigisterObserver("getgoodsdetails", this.getGoodsDetails, this);
    dataManager.getProductDetails(id);
  },
  /**获取商品详情的回调函数 */
  getGoodsDetails() {
    dataManager.UseNavigateTo("/pages/productdetails/productdetails", false);
  },

  /**确认删除订单 */
  onConfirmCancelOrder(data) {
    // this.data.setGoodsTip = ""
    dataManager.rigisterObserver("deleteordernos", this.onEditOrderComplete, this);
    dataManager.onCancelOrderDm(data.orderno);
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})